import { request } from '@/utils/request'
import { LoginType, ServiceType } from '@/config'
import type {
  RoleRequest,
  RoleResponse,
  RolePageParam,
  RolePermissionResponse,
  RolePermissionRequest,
  RolePermissionCodeResponse,
  RoleBoxParam,
  BoxDTO,
} from '@/types/systemRole'
import type { PageData } from '@/types/response'

export const roleService = {
  /** 创建角色 */
  createRole(data: RoleRequest): Promise<RoleResponse> {
    return request.post('/systemrole', {
      body: data,
    })
  },

  /** 修改角色 */
  modifyRole(data: RoleRequest): Promise<RoleResponse> {
    return request.put('/systemrole', {
      body: data,
    })
  },

  /** 删除角色 */
  deleteRole(roleId: string): Promise<RoleResponse> {
    return request.delete(`/systemrole/${roleId}`)
  },

  /** 获取角色详情 */
  getRoleById(roleId: string): Promise<RoleResponse> {
    return request.get(`/systemrole/${roleId}`, {
      customHeaders: { systemCode: 'hbteck' },
    })
  },

  /** 获取角色分页列表 */
  getRolePage(params: RolePageParam): Promise<PageData<RoleResponse>> {
    return request.post('/systemrole/list/page', {
      body: params,
    })
  },

  /** 修改角色状态 */
  modifyStatus(params: { roleId: string; roleStatus: number }): Promise<RoleResponse> {
    return request.put('/systemrole/status', {
      body: params,
    })
  },

  /** 获取当前用户角色权限信息 */
  getRolePermission(): Promise<RolePermissionResponse> {
    return request.get('/rolebase/role/permission', {
      serviceType: ServiceType.AUTH,
      customHeaders: { loginType: LoginType.ADMIN },
    })
  },

  /** 角色绑定权限 */
  bindRolePermission(data: RolePermissionRequest): Promise<string> {
    return request.put('/rolebase/role/permission', {
      body: data,
      serviceType: ServiceType.AUTH,
      customHeaders: { loginType: LoginType.ADMIN },
    })
  },

  /** 根据角色ID获取权限CODE */
  getPermissionCodesByRoleId(roleId: string): Promise<RolePermissionCodeResponse> {
    return request.get(`/rolebase/role/permission/code/${roleId}`, {
      serviceType: ServiceType.AUTH,
      customHeaders: { loginType: LoginType.ADMIN },
    })
  },

  /** 获取角色下拉框数据 */
  getRoleBox(params: RoleBoxParam = {}): Promise<BoxDTO[]> {
    return request.post('/systemrole/list/box', {
      body: params,
    })
  },
}
